<?php
$db = new Database;
$auth = Auth::getAuth('current_user');
$user_id = $auth['id'];
$company_id = $auth['company_id'];
if(isset($_POST['action'])){
    if($_POST['action']=="saveWorkFlow"){
        //FOR OBJECTS
        $object_id = explode(",",implode(",",$_POST['object_id']));
        $object_tops = explode(",",implode(",",$_POST['object_tops']));
        $object_lefts = explode(",",implode(",",$_POST['object_lefts']));
        $object_role = explode(",",implode(",",$_POST['object_role']));
        $object_status = explode(",",implode(",",$_POST['object_status']));
        $approverType = explode(",",implode(",",$_POST['approverType']));
        $buttonStatus = explode(",",implode(",",$_POST['buttonStatus']));
        //FOR LINES
        $line_id = explode(",",implode(",",$_POST['line_id']));
        $master_id = explode(",",implode(",",$_POST['master_id_workflow']));
        $child_id = explode(",",implode(",",$_POST['child_id']));
        $line_tops = explode(",",implode(",",$_POST['line_tops']));
        $line_lefts = explode(",",implode(",",$_POST['line_lefts']));
        $line_heights = explode(",",implode(",",$_POST['line_heights']));
        $line_widths = explode(",",implode(",",$_POST['line_widths']));
        $line_margin_tops = explode(",",implode(",",$_POST['line_margin_tops']));
        $line_margin_lefts = explode(",",implode(",",$_POST['line_margin_lefts']));
        $line_axis = explode(",",implode(",",$_POST['line_axis']));
        $line_type = explode(",",implode(",",$_POST['line_type']));
        $confRequest = $_POST['confRequest'];
        //inserts
        $workFlowDetails = array(
                "date"=>"",
                "is_active"=>1,
                "config_status"=>$confRequest
                );
        $insertworkFlowDetails = $db->insert("tbworkflow",$workFlowDetails);
        for($obj=0;$obj<count($object_id);$obj++){
            $workFlowObjects = array(
                "worflow_id"=>$insertworkFlowDetails,
                "object_id"=>$object_id[$obj],
                "tops"=>$object_tops[$obj],
                "lefts"=>$object_lefts[$obj],
                "role"=>$object_role[$obj],
                "status"=>$object_status[$obj],
                "approverType"=>$approverType[$obj],
                "buttonStatus"=>$buttonStatus[$obj]
                );
            $insertworkFlowObject = $db->insert("tbworflow_obects",$workFlowObjects);
        }
        
        for($lines=0;$lines<count($line_id);$lines++){
            $workFlowLines = array(
                "line_id"=>$line_id[$lines],
                "worflow_id"=>$insertworkFlowDetails,
                "master_id"=>$master_id[$lines],
                "child_id"=>$child_id[$lines],
                "tops"=>$line_tops[$lines],
                "lefts"=>$line_lefts[$lines],
                "heights"=>$line_heights[$lines],
                "widths"=>$line_widths[$lines],
                "margin_tops"=>$line_margin_tops[$lines],
                "margin_lefts"=>$line_margin_lefts[$lines],
                "axis"=>$line_axis[$lines],
                "line_type"=>$line_type[$lines],
                );
            $insertworkFlowLines = $db->insert("tbworkflow_line",$workFlowLines);
        }
        $form_id = $_POST['form_id'];
        $update = array(
            "workflow_id"=>$insertworkFlowDetails
            );
        $condition = array(
            "id"=>$form_id
            );
        $db->update("tbform",$update,$condition);
        
        //SEQUENCE OF WORFLOW
        /*$loopRange = $db->query("select distinct(master_id) FROM tbworkflow_line where worflow_id = '$insertworkFlowDetails'","numrows");
        for($a=0;$a<$loopRange;$a++){
            if($a==0){
                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = 'flowspan_1_worklow' and worflow_id = '$insertworkFlowDetails'","row");    
            }else{
                $master = $db->query("SELECT child_id FROM tbworkflow_line where master_id = '".$master['child_id']."' and worflow_id = '$insertworkFlowDetails'","row");    
            }
            $queryApprover = $db->query("SELECT role FROM tbworflow_obects where object_id='".$master['child_id']."'","row");
            echo $queryApprover['role'];
        }*/
    }else if($_POST['action']=="validateWorkflow"){
        $object_id = explode(",",implode(",",$_POST['object_id']));
        $line_id = explode(",",implode(",",$_POST['line_id']));
        $nodeApprovers = (count($object_id)-1)*4;
        if($nodeApprovers==count($line_id)){
            echo 1;
        }
    }
}
?>